﻿using AutoMapper;
using PowerHub.MES.Common.NumberingCenter;
using PowerHub.MES.DTO.Crew.DTO;
using PowerHub.MES.DTO.Crew.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.ICrew;
using PowerHub.MES.IServices.ICrew;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.DTO.ProductPlanning.DTO;
using PowerHub.MES.Entities.ProductPlanning;
using PowerHub.MES.DTO.ProductPlanning.QueryDTO;
using PowerHub.MES.IServices.IProductPlanning;
using PowerHub.MES.IRepository.IProductPlanning;

namespace PowerHub.MES.Services.ProductPlanning
{

    /// <summary>
    /// 原料接口实现层
    /// </summary>
    public class ProductPlanning_MaterialServices : BaseServices<ProductPlanning_Material>, IProductPlanning_MaterialServices
    {
        private IProductPlanning_MaterialRepository _iProductPlanning_MaterialRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public ProductPlanning_MaterialServices(PowerHubMesContext powerHubMesContext, IProductPlanning_MaterialRepository iProductPlanning_MaterialRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iProductPlanning_MaterialRepository = iProductPlanning_MaterialRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取原料列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProductPlanning_Material>>> GetProductPlanning_MaterialPageListAsync(QueryProductPlanning_MaterialDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iProductPlanning_MaterialRepository.GetProductPlanning_MaterialPageListAsync(query);
        }

        /// <summary>
        /// 添加原料
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProductPlanning_MaterialAsync(ProductPlanning_MaterialDTO query)
        {
            ProductPlanning_Material foundation = await GetEntityAsync(d => d.MaterialsName.Equals(query.MaterialsName) && d.IsDel == 0);
            if (foundation != null)
            {
                throw new Exception("你输入的原料名称已存在！");
            }
            //实体转换
            var ProductPlanning_Material = _mapper.Map<ProductPlanning_Material>(query);
            ProductPlanning_Material.Id = Guid.NewGuid().ToString();
            ProductPlanning_Material.MaterialsCode = string.IsNullOrEmpty(query.MaterialsCode) || query.MaterialsCode.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.MaterialsName) : query.MaterialsCode;
            ProductPlanning_Material.Creator = _userServices.UserName;

            return await _iProductPlanning_MaterialRepository.CreateProductPlanning_MaterialAsync(ProductPlanning_Material);
        }


        /// <summary>
        /// 批量删除原料
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProductPlanning_MaterialAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iProductPlanning_MaterialRepository.DeletionProductPlanning_MaterialAsync(ids);
        }


        /// <summary>
        /// 更新原料
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProductPlanning_MaterialAsync(ProductPlanning_MaterialDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取原料信息
            ProductPlanning_Material ProductPlanning_Material = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (ProductPlanning_Material == null)
            {
                throw new Exception("更新原料信息发生了异常,请刷新页面后试试");
            }
            ProductPlanning_Material.MaterialsName = query.MaterialsName;
            ProductPlanning_Material.ModelNumber = query.ModelNumber;
            ProductPlanning_Material.Specification = query.Specification;
            ProductPlanning_Material.Supplier = query.Supplier;
            ProductPlanning_Material.Updator = _userServices.UserName;

            return await _iProductPlanning_MaterialRepository.UpdateProductPlanning_MaterialAsync(ProductPlanning_Material);

        }
    }
}
